package com.zmn.oms.services.impl.statement;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.model.dto.work.modify.must.OrderStatementDTO;
import com.zmn.oms.model.entity.statement.OrderStatement;
import com.zmn.oms.persistence.interfaces.statement.OrderStatementDao;
import com.zmn.oms.services.interfaces.statement.OrderStatementService;
import io.shardingsphere.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单结算信息
 * @author: LuJia
 * @version: v1.0
 * @since: 2019/12/26 11:51
 **/
@Service
@Slf4j
public class OrderStatementServiceImpl implements OrderStatementService {

    @Resource
    OrderStatementDao orderStatementDao;

    @Override
    public List<OrderStatement> listOrderStatementByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderStatementDao.listOrderStatementByOrderIdList(orderIdList);
        }
    }

    @Override
    public OrderStatement findOrderStatementByOrderId(Long orderId, Long workId) {
        OrderStatement dbOrderStatement = orderStatementDao.findByKey(orderId, workId);
        return dbOrderStatement;
    }

    @Override
    public int insert(OrderStatement statement) {
        return orderStatementDao.insert(statement);
    }

    @Override
    public void updateStatement(OrderStatementDTO e) {
        Long orderId = e.getOrderId();
        OrderStatement dbOrderStatement = orderStatementDao.findByKey(orderId, orderId);
        OrderStatement orderStatement = this.dtoToEntity(e);
        if (dbOrderStatement == null) {
            try {
                orderStatement.setCreateTime(DateUtil.getNow());
                orderStatementDao.insert(orderStatement);// 唯一索引做幂等验证
            } catch (DuplicateKeyException ex) {
                orderStatementDao.updateByKey(orderStatement);
                log.error("[{}]主键重复", orderId);
            }
        } else {
            orderStatementDao.updateByKey(orderStatement);
        }
    }

    private OrderStatement dtoToEntity(OrderStatementDTO orderStatementDTO) {
        OrderStatement orderStatement = new OrderStatement();
        orderStatement.setWorkId(orderStatementDTO.getOrderId());
        orderStatement.setOrderId(orderStatementDTO.getOrderId());
        // 渠道结算
        orderStatement.setChannelStatementStatus(NumberUtil.isNotNullOrZero(orderStatementDTO.getChannelStatementAmount()) ? GlobalConsts.YES : GlobalConsts.NO);
        orderStatement.setChannelStatementAmount(orderStatementDTO.getChannelStatementAmount());
        orderStatement.setChannelStatementRemark(orderStatementDTO.getChannelStatementRemark());
        // 服务商结算
        orderStatement.setSpStatementStatus(NumberUtil.isNotNullOrZero(orderStatementDTO.getAmount()) ? GlobalConsts.YES : GlobalConsts.NO);
        orderStatement.setSpStatementAmount(orderStatementDTO.getAmount());
        orderStatement.setSpStatementTime(orderStatementDTO.getPayTime());

        //渠道可结算的时间
        orderStatement.setChannelStatementCanTime(orderStatementDTO.getChannelStatementCanTime());

        orderStatement.setUpdater("");
        orderStatement.setUpdateTime(DateUtil.getNow());
        return orderStatement;
    }
}
